<template>
  <div class="report-book-container">
    <el-card class="report-book-card">
      <template #header>
        <div class="card-header">
          <span>举报书籍管理</span>
        </div>
      </template>
      
      <el-table :data="reportList" style="width: 100%">
        <el-table-column prop="id" label="ID" width="80" />
        <el-table-column prop="bookName" label="书籍名称" />
        <el-table-column prop="reportUser" label="举报用户" />
        <el-table-column prop="reportReason" label="举报原因" />
        <el-table-column prop="reportTime" label="举报时间" width="180" />
        <el-table-column prop="status" label="状态" width="100">
          <template #default="scope">
            <el-tag :type="scope.row.status === '待处理' ? 'warning' : scope.row.status === '已处理' ? 'success' : 'danger'">
              {{ scope.row.status }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="180">
          <template #default="scope">
            <el-button type="primary" size="small" @click="handleProcess(scope.row)">处理</el-button>
            <el-button type="danger" size="small" @click="handleDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <!-- 处理举报对话框 -->
    <el-dialog
      v-model="dialogVisible"
      title="处理举报"
      width="500px"
    >
      <el-form :model="processForm" :rules="rules" ref="processFormRef" label-width="80px">
        <el-form-item label="处理结果" prop="result">
          <el-select v-model="processForm.result" placeholder="请选择处理结果">
            <el-option label="属实，下架书籍" value="remove" />
            <el-option label="不属实，驳回举报" value="reject" />
          </el-select>
        </el-form-item>
        <el-form-item label="处理说明" prop="description">
          <el-input
            v-model="processForm.description"
            type="textarea"
            :rows="3"
            placeholder="请输入处理说明"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="handleSubmit">确定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'

// 举报列表数据
const reportList = ref([
  {
    id: 1,
    bookName: '示例书籍1',
    reportUser: '用户A',
    reportReason: '内容违规',
    reportTime: '2024-04-05 10:00:00',
    status: '待处理'
  },
  {
    id: 2,
    bookName: '示例书籍2',
    reportUser: '用户B',
    reportReason: '版权问题',
    reportTime: '2024-04-05 11:00:00',
    status: '已处理'
  }
])

// 对话框相关
const dialogVisible = ref(false)
const processFormRef = ref(null)

// 表单数据
const processForm = reactive({
  result: '',
  description: ''
})

// 表单验证规则
const rules = {
  result: [
    { required: true, message: '请选择处理结果', trigger: 'change' }
  ],
  description: [
    { required: true, message: '请输入处理说明', trigger: 'blur' },
    { min: 5, max: 200, message: '长度在 5 到 200 个字符', trigger: 'blur' }
  ]
}

// 处理举报
const handleProcess = (row) => {
  dialogVisible.value = true
}

// 删除举报
const handleDelete = (row) => {
  ElMessageBox.confirm(
    '确定要删除该举报记录吗？',
    '警告',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning',
    }
  ).then(() => {
    // TODO: 调用删除接口
    ElMessage.success('删除成功')
  }).catch(() => {})
}

// 提交处理结果
const handleSubmit = () => {
  processFormRef.value.validate((valid) => {
    if (valid) {
      // TODO: 调用处理接口
      ElMessage.success('处理成功')
      dialogVisible.value = false
    }
  })
}
</script>

<style scoped lang="scss">
.report-book-container {
  padding: 20px;
}

.report-book-card {
  .card-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
  }
}

.dialog-footer {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
}
</style>